package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)


func main() {
	students := []string{"AA", "BB", "CC", "DD"}
	record := make(map[string]int)
	file, err := os.Open("name.txt")
	if err == nil {
		defer file.Close()
		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			line := scanner.Text()
			parts := strings.Split(line, ":")
			name := parts[0]
			count, _ := strconv.Atoi(parts[1])
			record[name] = count
		}
	}


	rand.Seed(time.Now().UnixNano())
	reader := bufio.NewReader(os.Stdin)
	for {
		fmt.Print("按任意键继续，按q退出:")
		input, _ := reader.ReadString('\n')
		if input == "q\n" {
			break
		}

		index := rand.Intn(len(students))
		name := students[index]
		record[name]++
		fmt.Println(name)
		file, _ = os.Create("name.txt")
		defer file.Close()
		writer := bufio.NewWriter(file)
		for name, count := range record {
			line := fmt.Sprintf("%s:%d\n", name, count)
			writer.WriteString(line)
		}
		writer.Flush()

		fmt.Println(record)
	}


}